package com.darren.archive.controller;

import com.darren.archive.entity.SysDictionary;
import com.darren.archive.service.SysDictionaryService;
import com.darren.framework.bean.ResponseBean;
import com.darren.framework.controller.BaseController;
import com.darren.framework.utils.Constant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Api(value = "系统字典", tags = "系统字典接口", description = "系统字典接口")
@Controller
public class SysDictionaryController extends BaseController {

    private Logger logger = LogManager.getLogger(SysDictionaryController.class);

    @Autowired
    private SysDictionaryService sysDictionaryService;


    /**
     * 根据类别名称模糊查找系统字典
     *
     * @param categoryName 类别名称
     * @param pageNum      页号
     * @param pageSize     页大小
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "categoryName", value = "类别名称", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "pageNum", value = "页号", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "页大小", paramType = "query", dataType = "Integer")
    })
    @ApiOperation(value = "查询字典", notes = "根据类别名称模糊查找系统字典")
    @RequiresPermissions("dictionary:view")
    @GetMapping("/sys/dictionary/list")
    @ResponseBody
    public ResponseBean dictionaryList(@RequestParam(name = "categoryName", required = false, defaultValue = "") String categoryName,
                                       @RequestParam(name = "pageNum", required = false, defaultValue = "0") Integer pageNum,
                                       @RequestParam(name = "pageSize", required = false, defaultValue = Constant.PAGE_SIZE_STRING) Integer pageSize) {
        Pageable pageable = PageRequest.of(pageNum, pageSize);
        return ResponseBean.isOk(sysDictionaryService.findByCategoryName("%" + categoryName + "%", pageable));
    }


    /**
     * 根据主键ID,查询对应的字典
     *
     * @return 字典
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "字典主键 id，必填", paramType = "path", dataType = "Long")
    })
    @RequiresPermissions("dictionary:view")
    @ApiOperation(value = "字典查询", notes = "根据主键ID,查询对应的字典,参数作为URL的一部分，如：http://localhost/sys/dictionary/{id}")
    @GetMapping("/sys/dictionary/{id}")
    @ResponseBody
    public ResponseBean getById(@PathVariable(name = "id") Long id) {
        return ResponseBean.isOk(sysDictionaryService.findById(id));
    }

    /**
     * 根据主键ID,删除对应的字典
     *
     * @return 字典
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "字典主键 id，必填", paramType = "path", dataType = "Long")
    })
    @RequiresPermissions("dictionary:edit")
    @ApiOperation(value = "字典删除", notes = "根据主键ID,删除对应的字典,参数作为URL的一部分，如：http://localhost/sys/dictionary/{id}")
    @DeleteMapping("/sys/dictionary/{id}")
    @ResponseBody
    public ResponseBean getDictionaryByCategoryCode(@PathVariable(name = "id") Long id) {
        sysDictionaryService.deleteById(id);
        return ResponseBean.isOk();
    }


    /**
     * 字典查询，根据字典类别编码，查询对应的字典
     *
     * @return 字典
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "字典类别编码 code，必填", paramType = "path", dataType = "String")
    })
    @RequiresPermissions("dictionary:view")
    @ApiOperation(value = "字典查询", notes = "字典查询，根据字典类别编码，查询对应的字典,参数作为URL的一部分，如：http://localhost/sys/dictionary/code/{code}")
    @GetMapping("/sys/dictionary/code/{code}")
    @ResponseBody
    public ResponseBean getDictionaryByCategoryCode(@PathVariable(name = "code", required = true) String code) {
        return ResponseBean.isOk(sysDictionaryService.findByCategoryCode(code));
    }


    /**
     * 保存字典信息
     *
     * @param record 字典信息
     * @return
     */
    @ResponseBody
    @RequiresPermissions("dictionary:edit")
    @ApiOperation(value = "保存字典信息", notes = "保存字典信息 SysUser，如果 id 为空，则新增；id 不为空，则修改")
    @PostMapping(value = "/sys/dictionary/save", consumes = "application/json;charset=UTF-8")
    public ResponseBean save(@RequestBody @ApiParam(name = "保存字典信息", value = "传入json格式", required = true) SysDictionary record) {
        return ResponseBean.isOk(sysDictionaryService.save(record));
    }


}
